package com.service.Impl;

import com.mapper.CTicketPlanMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CTicketPlan;
import com.pojo.vo.CTicketPlanVo;
import com.pojo.vo.CTicketRoundVo;
import com.pojo.vo.CTicketVo;
import com.service.CTicketPlanService;
import com.service.CTicketRoundService;
import com.service.CTicketService;
import com.util.PojoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("CTicketPlanService")
public class CTicketPlanServiceImpl implements CTicketPlanService {
    @Autowired
    CTicketPlanMapper cTicketPlanMapper;
    @Autowired
    CTicketRoundService cTicketRoundService;
    @Autowired
    CTicketService cTicketService;

    @Override
    public Message<List<CTicketPlanVo>> list(CTicketPlanVo cTicketPlanVo) {
        QueryWrapper<CTicketPlan> queryWrapper = new QueryWrapper<>();
        if (cTicketPlanVo != null) {
            if (!StringUtils.isEmpty(cTicketPlanVo.getUserNum())) {
                queryWrapper.eq("user_num", cTicketPlanVo.getUserNum());
            }
            if (!StringUtils.isEmpty(cTicketPlanVo.getPlanId())) {
                queryWrapper.eq("plan_id", cTicketPlanVo.getPlanId());
            }
            if (!StringUtils.isEmpty(cTicketPlanVo.getState())) {
                queryWrapper.eq("state", cTicketPlanVo.getState());
            }
        }
        List<CTicketPlan> cTicketPlans = cTicketPlanMapper.selectList(queryWrapper);
        List<CTicketPlanVo> list = new ArrayList<>();
        for (int i = 0; i < cTicketPlans.size(); i++) {
            CTicketPlanVo cTicketPlanVo1 = PojoUtil.copyObject(cTicketPlans.get(i), CTicketPlanVo.class);
            list.add(cTicketPlanVo1);
        }
        return Message.successMessage(1L, list);
    }

    @Override
    public Message<CTicketPlanVo> selectOne(CTicketPlanVo cTicketPlanVo) {
        QueryWrapper<CTicketPlan> queryWrapper = new QueryWrapper<>();
        if (cTicketPlanVo != null) {
            if (!StringUtils.isEmpty(cTicketPlanVo.getId())) {
                queryWrapper.eq("id", cTicketPlanVo.getId());
            }
            if (!StringUtils.isEmpty(cTicketPlanVo.getPlanId())) {
                queryWrapper.eq("plan_id", cTicketPlanVo.getPlanId());
            }
            if (!StringUtils.isEmpty(cTicketPlanVo.getUserNum())) {
                queryWrapper.eq("user_num", cTicketPlanVo.getUserNum());
            }
        }
        CTicketPlan cTicketPlan = cTicketPlanMapper.selectOne(queryWrapper);
        if (cTicketPlan != null)
            return Message.successMessage(1L, PojoUtil.copyObject(cTicketPlan, CTicketPlanVo.class));
        return Message.successMessage(1L, null);
    }

    @Override
    public Message<CTicketPlanVo> update(CTicketPlanVo cTicketPlanVo) {
        if (!StringUtils.isEmpty(cTicketPlanVo.getStartTime())) {
            long time = cTicketPlanVo.getStartTime().getTime();
            long time1 = new Date().getTime();
            //如果开始时间小于现在时间设置场次状态为进行中
            if (cTicketPlanVo.getState() == null && (time1 >= time)) cTicketPlanVo.setState(2L);
        }
        int i = cTicketPlanMapper.updateById(PojoUtil.copyObject(cTicketPlanVo, CTicketPlan.class));
        if (i == 0) return Message.errorMessage("1001", "更新失败");
        else return Message.successMessage(1L, cTicketPlanVo);
    }

    @Override
    public Message<CTicketPlanVo> add(CTicketPlanVo cTicketPlanVo) {
        cTicketPlanVo.setPlanId(design());
        //如果开始时间小于现在时间设置场次状态为进行中
        // 开始时间设置为必填
//        if (cTicketPlanVo.getStartTime().getTime() >= new Date().getTime()){
//            cTicketPlanVo.setState(2L);
//        }else {
//            cTicketPlanVo.setState(1L);
//        }
        int i = cTicketPlanMapper.insert(PojoUtil.copyObject(cTicketPlanVo, CTicketPlan.class));
        if (i == 0) return Message.errorMessage("1001", "场次添加失败");
        //添加c_ticket_round表数据
        CTicketRoundVo cTicketRoundVo = new CTicketRoundVo();
        cTicketRoundVo.setPlanId(cTicketPlanVo.getPlanId());
        cTicketRoundVo.setStartTime(cTicketPlanVo.getStartTime());
        cTicketRoundVo.setUserCount(0L);
        cTicketRoundService.add(cTicketRoundVo);
        return Message.successMessage(1L, cTicketPlanVo);
    }

    @Override
    public Message<CTicketPlanVo> delete(Long id) {
        //删除场次的时候把所有参与该批次的用户记录删除
        CTicketPlanVo cTicketPlanVo1 = new CTicketPlanVo();
        cTicketPlanVo1.setId(id);
        CTicketPlanVo cTicketPlanVo = selectOne(cTicketPlanVo1).getData();
        Long planId = cTicketPlanVo.getPlanId();
        CTicketVo cTicketVo = new CTicketVo();
        cTicketVo.setPlanId(planId);
        List<CTicketVo> cTicketVos = cTicketService.list(cTicketVo).getData();//查出所有参加的用户
        if (!cTicketVos.isEmpty()) {
            for (int i = 0; i < cTicketVos.size(); i++) {
                Long id1 = cTicketVos.get(i).getId();
                cTicketService.delete(id1);
            }
        }
        int i = cTicketPlanMapper.deleteById(id);
        if (i == 0) return Message.errorMessage("1001", "删除失败");
        else return Message.successMessage(1L, null);
    }

    @Override
    public Long design() {
        long time = new Date().getTime();
        time = time / 1000;
        time += Math.random() * 10;
        return time;
    }

}
